# -*- coding: utf-8 -*-
# Create your views here.
from django.http import Http404, HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from b52.home.forms.registrationForm import RegistrationForm
#from b52.home.forms import *
from django.template import RequestContext
from b52.home.models import *
import logging
import datetime
#from b52.home.models import User

logger = logging.getLogger(__name__)

def generate(request, name):
    user = User.objects.create_user(name, 'lennon@thebeatles.com', 'johnpassword')
    user.is_staff = False
    user.save()
    html = "<html> <body> User with name was created: %s   </body></html>" % (name)
    return HttpResponse(html)
    
    
def page(request, num="1"):

    html = "<html> <body> show goods with id: %s arg2  </body></html>" % (num)
    return HttpResponse(html)

def index_b52(request):
    now = datetime.datetime.now()
    return render_to_response('index_b52.html', {'current_date': now})

    
# there are anything is ok. Anything is working 
def category(request, num):

    
    return render_to_response('category.html',
                {
                    'data': {
                        'data': num,
                        'top': 'top top top',
                        'title': 'This is Categorys',
                    }
                }
    )


def goods(request, page='-1', UID='-1'):
    data = ''
    logger.error('This is one goods with id ' + page)
    logger.error(UID)
    #logger.error('This is one goods with id %' % (UID))
     
    if (page == '-1'):
        #user want to see only one goods by UID
        data = {
                'data': {
                         'UID': UID,
                         'top': 'top top top',
                         'title': 'This is one goods with id %' + UID,
                     }
                 } 
    else:
                #user want to see only one goods by UID
        data = {
                'data': {
                         'page': page,
                         'top': 'top top top',
                         'title': 'There are a lot of goods on page: %' + page,
                         }
                 } 
        
    return render_to_response('goods.html', data )

def UserRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password=form.cleaned_data['password'])
            user.save()
            user_profile = UserProfile(user=user, username=form.cleaned_data['username'], favorite_animal=form.cleaned_data['favorite_animal'])
            user_profile.save()
            return HttpResponseRedirect('/profile/')
        else:
            return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
    else:
        """user is not submetting the form, show them a blank registration form """
        form = RegistrationForm()
        context = {'form':form}
        return render_to_response('register.html', context, context_instance=RequestContext(request))

@login_required
def Profile(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/')
    user_profile = request.user.get_profile
    context = {'user_profile': user_profile}
    return render_to_response('profile.html', context, context_instance=RequestContext(request))

def LoginRequest(request):
    if request.user.is_athenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user_profile = authenticate(username=username, password=password)
            if user_profile is not None:
                login(request, drinker)
                return HttpResponseRedirect('/profile/')
            else:
                return render_to_response('login.html', {'form':form}, context_instance=RequestContext(request))
        else:
            return render_to_response('login.html', {'form' : form}, context_instance=RequestContext(request))
    else:
        """user is not submitting the form, show the login form"""
        form = LoginForm()
        context = {'form': form}
        return render_to_response('login.html', context, context_instance=RequestContext(request))
            
def LogoutRequest(request):
    logout(request)
    return HttpResponseRedirect('/')

#def registration(request):
#    if request.method == 'POST':
#        form = RegistrationForm(data=request.POST)
#        #if form.is_valid():
#        new_user = form.save()
#        return HttpResponseRedirect("/accounts/login/")
#    else:
#        form = RegistrationForm()
#    return render_to_response('registration.html', { 'form': form })


    
    
    
    
    
    